//
//  AppManager.m
//  ImGuider
//
//  Created by llt on 2017/4/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AppManager.h"
#import "NetClient.h"
#import "AppDelegate.h"
#import "LaunchVC.h"
#import "searchCountyList.h"


#define kCityVersion @"cityVersion" // 本地存储的城市版本号
#define kCountryVersion @"countryVersion" // 本地存储的国家版本号
#define kNetVersion @"netVersion"
#define kLastCityID @"cityID" // 上次获取的城市ID
#define kLastRecomend @"Recomend" // 上次获取的推荐城市URL
#define kCountryList @"CountryList"// 获取的按国家分组的城市


#define kAskPlay @"kAskPlay"
#define kAskDownload @"kAskDownload"
#define KAutomaticPlay @"automaticPlay"
#define kOffReceiver @"kOffReceiver"
#define kCountryCode @"kCountryCode"

#define kAllPlayWhenListen @"kAllPlayWhenListen"


@interface AppManager()

@end

@implementation AppManager

@synthesize baseURLStr = _baseURLStr,cityver = _cityver, countryver = _countryver, recomendParams = _recomendParams, lastCityID = _lastCityID, currentCountryCode = _currentCountryCode, language = _language;

+ (instancetype)sharedManager {
    
    static AppManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[AppManager alloc] init];
        
    });
    return _sharedManager;
}


- (NSString *)baseURLStr {
    
    if (!_baseURLStr) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        _baseURLStr = [defaults objectForKey:kBaseURLStr] ?: kBaseURLStr;
    }
    return _baseURLStr;
}

- (void)setBaseURLStr:(NSString *)baseURLStr {
    
    if (baseURLStr.length == 0 || [_baseURLStr isEqualToString:baseURLStr]) {
        
        return;
    }
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:baseURLStr forKey:kBaseURLStr];
    [defaults synchronize];
    
    _baseURLStr = baseURLStr;
    [NetClient changeClient];
}

- (void)setCityver:(NSString *)cityver {
    
    if (![_cityver isEqualToString:cityver]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:cityver forKey:kCityVersion];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        _cityver = cityver;
    }
}

- (void)setCountryList:(NSArray *)countryList {
    
    [[NSUserDefaults standardUserDefaults] setObject:countryList forKey:kCountryList];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray *)countryList {
    
    NSArray *arr = [NSArray yy_modelArrayWithClass:[searchCountyList class] json:[[NSUserDefaults standardUserDefaults] arrayForKey:kCountryList]];
    
    NSArray *sortArray = [arr sortedArrayUsingComparator:^NSComparisonResult(searchCountyList *obj1, searchCountyList *obj2) {
        
        NSString *idx1 = [IGUtils firstCharactor:obj1.smartName];
        NSString *idx2 = [IGUtils firstCharactor:obj2.smartName];
        
        return [idx1 compare:idx2];
    }];
    
    return sortArray;
}

- (NSString *)cityver {
    
    if (!_cityver) {
        _cityver = [[NSUserDefaults standardUserDefaults] objectForKey:kCityVersion];
    }
    return _cityver;
}

- (void)setCountryver:(NSString *)countryver {
    if (![_countryver isEqualToString:countryver]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:countryver forKey:kCountryVersion];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        _countryver = countryver;
    }
}

- (NSString *)countryver {
    
    if (!_countryver) {
        _countryver = [[NSUserDefaults standardUserDefaults] objectForKey:kCountryVersion];
    }
    return _countryver;
}


- (void)setVersion:(VersionModel *)version {
    
    [[NSUserDefaults standardUserDefaults] setObject:[version yy_modelToJSONString] forKey:kNetVersion];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (VersionModel *)version {
    
    NSString *versionJson = [[NSUserDefaults standardUserDefaults] objectForKey:kNetVersion];
    
    return [VersionModel yy_modelWithJSON:versionJson];
}



- (void)setRecomendParams:(NSDictionary *)recomendParams {
    
    if (![_recomendParams isEqualToDictionary:recomendParams]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:recomendParams forKey:kLastRecomend];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        _recomendParams = recomendParams;
    }
}

- (NSDictionary *)recomendParams {
    
    if (!_recomendParams) {
        _recomendParams = [[NSUserDefaults standardUserDefaults] objectForKey:kLastRecomend];
    }
    return _recomendParams;
}

- (void)setLastCityID:(NSNumber *)lastCityID {
    
    if (![_lastCityID isEqualToNumber:lastCityID]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:lastCityID forKey:kLastCityID];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        _lastCityID = lastCityID;
    }
}


- (NSNumber *)lastCityID {
    
    if (!_lastCityID) {
        _lastCityID = [[NSUserDefaults standardUserDefaults] objectForKey:kLastCityID];
    }
    return _lastCityID;
}

- (BOOL)allowWhenDownload {
    
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAskDownload];
}

- (void)setAllowWhenDownload:(BOOL)allowWhenDownload {
    
    [[NSUserDefaults standardUserDefaults] setBool:allowWhenDownload forKey:kAskDownload];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setIsOffReceiverWhenListen:(BOOL)isOffReceiverWhenListen {
    [[NSUserDefaults standardUserDefaults] setBool:isOffReceiverWhenListen forKey:kOffReceiver];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isOffReceiverWhenListen {
    
    return [[NSUserDefaults standardUserDefaults] boolForKey:kOffReceiver];
}

- (void)setAllPlayWhenListen:(BOOL)allPlayWhenListen {
    
    [[NSUserDefaults standardUserDefaults] setBool:allPlayWhenListen forKey:kAllPlayWhenListen];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)allPlayWhenListen {
    
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAllPlayWhenListen];
}

- (BOOL)allowWhenPlay {
    
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAskPlay];
}

- (BOOL)automaticPlay {
    
    
    if ([[NSUserDefaults standardUserDefaults] valueForKey:KAutomaticPlay]) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:KAutomaticPlay];
    }
    
    return YES;
}

- (void)setAutomaticPlay:(BOOL)automaticPlay {
    
    [[NSUserDefaults standardUserDefaults] setBool:automaticPlay forKey:KAutomaticPlay];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)setAllowWhenPlay:(BOOL)allowWhenPlay {
    
    [[NSUserDefaults standardUserDefaults] setBool:allowWhenPlay forKey:kAskPlay];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setCurrentCountryCode:(NSString *)currentCountryCode {
    
    if (![_currentCountryCode isEqualToString:currentCountryCode]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:currentCountryCode forKey:kCountryCode];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    _currentCountryCode = currentCountryCode;
}

- (NSString *)currentCountryCode {
    
    if (!_currentCountryCode) {
        _currentCountryCode = [[NSUserDefaults standardUserDefaults] objectForKey:kCountryCode];
    }
    
    if (!_currentCountryCode) {
        
        _currentCountryCode = [[NSLocale currentLocale] localeIdentifier];
    }
    
    return _currentCountryCode;
}

- (NSString *)language {
    
    
    if (_language) {
        return _language;
    }
    
    NSArray *langArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"AppleLanguages"];
    return [langArray firstObject];
}

static NSBundle *bundle = nil;

- (void)setLanguage:(NSString *)language {

    language = [language lowercaseString];
    
    
    NSString *lanStr = @"en";
    
    if ([language hasPrefix:@"zh-hans"]) {
        
        lanStr = @"zh-Hans";
    } else if ([language hasPrefix:@"zh-hant"]) {
        
        if ([language hasSuffix:@"hk"]) {
            
             lanStr = @"zh-Hant-HK";
        } else {
            
            lanStr = @"zh-Hant";
        }
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:lanStr ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];
    
    _language = lanStr;
}

- (NSString *)get:(NSString *)key alter:(NSString *)alternate {
    
    return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end
